﻿//-----------------------------------------------------------------------
// <copyright file="ConsoleProjectViewModel.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Common.Model;
using ConsoleHoster.Common.ViewModel;
using ConsoleHoster.Model.Entities;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media;

namespace ConsoleHoster.ViewModel.Entities
{
	public class ConsoleProjectViewModel : ViewModelEntityBase<ConsoleProject>
	{
		private ObservableCollection<CommandDataViewModel> commands = null;

		public ConsoleProjectViewModel(ConsoleProject argModel)
			: base(argModel)
		{
			if (argModel.Commands != null)
			{
				this.commands = new ObservableCollection<CommandDataViewModel>(argModel.Commands.Select(item => new CommandDataViewModel(item)).OrderBy(item => item.Name));
			}
		}

		public ConsoleProjectViewModel()
			: this(new ConsoleProject())
		{

		}

		public ConsoleProjectViewModel(IWindowsShortcut argShortcut)
			: this()
		{
			if (argShortcut == null)
			{
				throw new ArgumentNullException("argShortcut");
			}

			this.Arguments = argShortcut.Arguments;
			this.Executable = argShortcut.Target;
			this.Name = System.IO.Path.GetFileNameWithoutExtension(argShortcut.ShortCutFile);
			this.WorkingDir = argShortcut.WorkingDirectory;
		}

		internal ConsoleProjectViewModel GetCopy()
		{
			return new ConsoleProjectViewModel(this.Model.GetCopy());
		}

		protected override void OnModelRequested(ConsoleProject argModel)
		{
			// As the model's commands list is not being updated, applying the updates to it now
			argModel.Commands = this.Commands == null ? null : new ObservableCollection<CommandData>(this.Commands.Select(item => item.Model));
		}

		#region Properties
		public string Name
		{
			get
			{
				return this.Model.Name;
			}
			set
			{
				if (value != this.Model.Name)
				{
					this.Model.Name = value;
					this.NotifyPropertyChange("Name");
					this.NotifyPropertyChange("UIFriendlyName");
				}
			}
		}

		public string UIFriendlyName
		{
			get
			{
				return this.Name.Replace("_", "__");
			}
		}

		public string Executable
		{
			get
			{
				return this.Model.Executable;
			}
			set
			{
				if (value != this.Model.Executable)
				{
					this.Model.Executable = value;
					this.NotifyPropertyChange("Executable");
				}
			}
		}

		public string Arguments
		{
			get
			{
				return this.Model.Arguments;
			}
			set
			{
				if (value != this.Model.Arguments)
				{
					this.Model.Arguments = value;
					this.NotifyPropertyChange("Arguments");
				}
			}
		}

		public string WorkingDir
		{
			get
			{
				return this.Model.WorkingDir;
			}
			set
			{
				if (value != this.Model.WorkingDir)
				{
					this.Model.WorkingDir = value;
					this.NotifyPropertyChange("WorkingDir");
				}
			}
		}

		public bool AutoLoad
		{
			get
			{
				return this.Model.AutoLoad;
			}
			set
			{
				if (value != this.Model.AutoLoad)
				{
					this.Model.AutoLoad = value;
					this.NotifyPropertyChange("AutoLoad");
				}
			}
		}

        public bool StoreOutput
        {
            get
            {
                return this.Model.StoreOutput;
            }
            set
            {
                if (value != this.Model.StoreOutput)
                {
                    this.Model.StoreOutput = value;
                    this.NotifyPropertyChange("StoreOutput");
                }
            }
        }

		public bool ShowExplorerTreeByDefault
		{
			get
			{
				return this.Model.ShowExplorerByDefault;
			}
			set
			{
				if (value != this.Model.ShowExplorerByDefault)
				{
					this.Model.ShowExplorerByDefault = value;
					this.NotifyPropertyChange("ShowExplorerTreeByDefault");
				}
			}
		}

		public ObservableCollection<CommandDataViewModel> Commands
		{
			get
			{
				return this.commands;
			}
			set
			{
				if (value != this.commands)
				{
					this.commands = value;
					this.NotifyPropertyChange("Commands");
				}
			}
		}

		public Color ProjectColor
		{
			get
			{
				return this.Model.ProjectColor;
			}
			set
			{
				this.Model.ProjectColor = value;
				this.NotifyPropertyChange("ProjectColor");
				this.NotifyPropertyChange("ProjectColorBrush");
			}
		}

		public Brush ProjectColorBrush
		{
			get
			{
				return new SolidColorBrush(this.ProjectColor);
			}
		}

		public Color ErrorMessageColor
		{
			get
			{
				return this.Model.ErrorMessageColor;
			}
			set
			{
				if (value != this.Model.ErrorMessageColor)
				{
					this.Model.ErrorMessageColor = value;
					this.NotifyPropertyChange("ErrorMessageColor");
				}
			}
		}

		public Color MessageColor
		{
			get
			{
				return this.Model.MessageColor;
			}
			set
			{
				if (value != this.Model.MessageColor)
				{
					this.Model.MessageColor = value;
					this.NotifyPropertyChange("MessageColor");
				}
			}
		}

		public Color BackgroundColor
		{
			get
			{
				return this.Model.BackgroundColor;
			}
			set
			{
				if (value != this.Model.BackgroundColor)
				{
					this.Model.BackgroundColor = value;
					this.NotifyPropertyChange("BackgroundColor");
				}
			}
		}

		public Color CaretColor
		{
			get
			{
				return this.Model.CaretColor;
			}
			set
			{
				if (value != this.Model.CaretColor)
				{
					this.Model.CaretColor = value;
					this.NotifyPropertyChange("CaretColor");
				}
			}
		}

		public bool IsEditable
		{
			get
			{
				return this.Model.IsEditable;
			}
			set
			{
				if (value != this.Model.IsEditable)
				{
					this.Model.IsEditable = value;
					this.NotifyPropertyChange("IsEditable");
				}
			}
		}
		#endregion
	}
}
